package qsl.app.MeizuBBS;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

class ViewListAdapter extends BaseAdapter {

	ArrayList<HashMap<String, String>> data;
	private final LayoutInflater inflater;

	public ViewListAdapter(LayoutInflater inflater,
			ArrayList<HashMap<String, String>> data) {
		super();
		this.inflater = inflater;
		this.data = data;
	}

	public int getCount() {
		return this.data.size();
	}

	public Object getItem(int position) {
		return position < data.size() ? data.get(position) : null;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = convertView == null ? inflater.inflate(
				R.layout.view_list, parent, false) : convertView;

		ImageGetter imgGetter = new Html.ImageGetter() {
			public Drawable getDrawable(String source) {
				try {
					Drawable drawable = Drawable.createFromStream(new URL(
							source).openStream(), "");
					drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
							drawable.getIntrinsicHeight());
					return drawable;
				} catch (Exception e) {
					return null;
				}
			}
		};

		HashMap<String, String> map = data.get(position);
		((TextView) convertView.findViewById(R.id.author)).setText(map
				.get("author"));
		((TextView) convertView.findViewById(R.id.time)).setText(map
				.get("time"));
		((TextView) convertView.findViewById(R.id.floor)).setText(map
				.get("index"));
		((TextView) convertView.findViewById(R.id.content)).setText(Html
				.fromHtml(map.get("content"), imgGetter, null/**/));

		return convertView;
	}
}
